package nacos;

import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.Service;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * 服务注册和发现
 * @author dzh
 * @date 2023/1/14 17:29
 */
@Slf4j
public class NacosServer {

    @Test
    public void registerServer() throws Exception {
        String serviceName = "nacos-server";
        NamingService naming = NamingFactory.createNamingService("localhost:8848");

        Instance instance = new Instance();
        instance.setIp("127.0.0.1");
        instance.setPort(80);
        instance.setHealthy(true);
        instance.setEphemeral(false);
        instance.setWeight(1.0);
        Map<String, String> instanceMeta = new HashMap<>();
        instanceMeta.put("site", "et1");
        instance.setMetadata(instanceMeta);

        naming.registerInstance(serviceName, instance);

        log.info("success");
    }
}
